<?php
/**
 * Created by PhpStorm.
 * User: UGLY
 * Date: 2017/12/7
 * Time: 14:04
 * Function:许个愿留言功能
 */

namespace app\blog\controller;

use think\Db;
use cmf\controller\HomeBaseController;
use think\Validate;
class messageController extends HomeBaseController
{
    //许愿页面
    public function index()
    {
        return $this->fetch();
    }
    //返回留言数据
    public function backmessage()
    {
        $data = Db::name('message')->order('id')->select();
        if($data){
            echo json_encode($data);
        }else{
            echo 2;
        }
    }
    //接收留言
    public function acceptmessage()
    {
        $request = request();
        if($request->isAjax()){
            $post = $request->post();
            //要验证的字段
            $rule = [
                'text' => 'require|max:240',
            ];
            //提交的数据
            $data = [
                'text'=>$post['text1'],
                'time'=>date("Y-m-d H:i:s",time()),
                'ip'=>$post['ip1']
            ];
            //出错的信息
            $msg = [
                'text.require' => '请输入你要发送的内容',
                'text.max'     => '内容最多不能超过240字',
            ];
            $validate = new Validate($rule,$msg);
            if($validate->check($data)){
                if(Db::name('message')->insert($data)){
                    $id = Db::name('message')->getLastInsID();
                    $data = [
                        'ids'=>$id,
                        'text3'=>$post['text1'],
                        'ip3'=>$post['ip1'],
                        'timee'=>date("Y-m-d H:i:s",time()),
                        'sta'=>1
                    ];
                    echo json_encode($data);
                }else{
                    echo 2;
                }
            }else{
                echo json_encode($validate->getError());
            }
        }else{
            echo '好好的，别闹！';
        }
    }
}